//------------------------------------------------------------------------------
// <copyright file="IRpcChannel.h" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

#pragma once

#include "RpcAnnotations.h"
#include "RpcSocketTransportMessage.h"
#include <functional>

namespace Microsoft { namespace Internal { namespace GamesTest { namespace Rpc { namespace Server
{
	class RpcResponse;

	/// <summary>
	/// Implementations of this interface listen for incoming requests and provide a mechanism to send back a response.
	/// </summary>
	class IRpcChannel
	{
	public:
		typedef std::function<void (std::weak_ptr<IRpcChannel>, RpcSocketTransportMessage)> RequestReceivedCallback;
		typedef std::function<void (std::weak_ptr<IRpcChannel>)> ChannelDisconnectedCallback;

		/// <summary>
		/// This functions sends a message to the client.
		/// </summary>
		/// <param name="message">The serialized RpcMessage to be sent to the client.</param>
		/// <param name="serverId">The Id of the RpcServer sending the message.</param>
		/// <param name="errorCode">The error code for any errors that were encountered while trying to process the request.</param>
		virtual GAMESTEST_ANNOTATE_HRESULT_METHOD HRESULT SendRpcMessage(const std::wstring& message, const GUID& serverId, HRESULT errorCode = S_OK) = 0;

		/// <summary>
		/// Indicates that the channel shall start listening for incoming requests and pass them along to the specified request handler.
		/// </summary>
		/// <param name="requestReceived">A function to be invoked when a request is received.</param>
		/// <param name="channelDisconnected">A function to be invoked when the channel has disconnected.</param>
		virtual GAMESTEST_ANNOTATE_HRESULT_METHOD HRESULT StartListening(RequestReceivedCallback requestReceived, ChannelDisconnectedCallback channelDisconnected) = 0;

		virtual ~IRpcChannel() { }
	};

}}}}}